package samples.spring.step2.beans;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * {@link BeanNameAware#setBeanName(String)}: 获取此bean在容器中的名称
 * {@link BeanClassLoaderAware#setBeanClassLoader(ClassLoader)}: 获取此bean在spring中初始化使用的classloader
 * {@link BeanFactoryAware#setBeanFactory(BeanFactory)}: 获取此bean在spring中所处的{@link BeanFactory}
 * {@link ApplicationContextAware#setApplicationContext(ApplicationContext)}:获取此bean的容器
 */
public class AwareSamplesBean implements BeanNameAware, BeanClassLoaderAware, BeanFactoryAware, ApplicationContextAware {

    public static final Logger LOGGER = LoggerFactory.getLogger(AwareSamplesBean.class);

    private String beanName;

    private ClassLoader beanClassLoader;

    private BeanFactory beanFactory;

    private ApplicationContext applicationContext;

    @Override
    public void setBeanName(String name) {
        LOGGER.info("获取bean在spring容器中的名称:{}", name);
        this.beanName = name;
    }

    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
        LOGGER.info("获取beanClassLoader: {}", classLoader);
        this.beanClassLoader = classLoader;
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        LOGGER.info("获取beanFactory : {}", beanFactory);
        this.beanFactory = beanFactory;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        LOGGER.info("获取 applicationContext: {}", applicationContext);
        this.applicationContext = applicationContext;
    }
}
